<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/codeblog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/codeblog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/codeblog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/codeblog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/codeblog/css/main.css">


<link rel="stylesheet" href="/codeblog/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/codeblog/lib/pace/pace-theme-minimal.min.css">
  <script src="/codeblog/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"skycoin.gitee.io","root":"/codeblog/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":true,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"valine","storage":true,"lazyload":false,"nav":null,"activeClass":"valine"},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="[toc] 1.理解对象引用值（或者对象）是某个特定引用类型的实例。在 es 中，引用类型是把数据和功能组织到一起的结构，经常被人错误的称为类。虽然从技术上来讲 js 是一门面向对象语言，但 es 却少传统的面向对象编程所具备的某些基本结构，包括类和接口。引用类型有时候也被称为对象定义，因为它们描述了自己的对象应有的属性和方法 对象被认为是某个特定引用类型的实例。新对象通过使用 new 操作符后跟">
<meta property="og:type" content="article">
<meta property="og:title" content="Javascript基础（四）：javascript基本引用类型">
<meta property="og:url" content="http://skycoin.gitee.io/codeblog/2021/02/27/%E5%9F%BA%E6%9C%AC%E5%BC%95%E7%94%A8%E7%B1%BB%E5%9E%8B/index.html">
<meta property="og:site_name" content="若水的前端博客">
<meta property="og:description" content="[toc] 1.理解对象引用值（或者对象）是某个特定引用类型的实例。在 es 中，引用类型是把数据和功能组织到一起的结构，经常被人错误的称为类。虽然从技术上来讲 js 是一门面向对象语言，但 es 却少传统的面向对象编程所具备的某些基本结构，包括类和接口。引用类型有时候也被称为对象定义，因为它们描述了自己的对象应有的属性和方法 对象被认为是某个特定引用类型的实例。新对象通过使用 new 操作符后跟">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-02-27T02:14:52.000Z">
<meta property="article:modified_time" content="2021-04-15T01:25:56.770Z">
<meta property="article:author" content="若说">
<meta property="article:tag" content="前端">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://skycoin.gitee.io/codeblog/2021/02/27/%E5%9F%BA%E6%9C%AC%E5%BC%95%E7%94%A8%E7%B1%BB%E5%9E%8B/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Javascript基础（四）：javascript基本引用类型 | 若水的前端博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/codeblog/atom.xml" title="若水的前端博客" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>
    <a target="_blank" rel="noopener" href="https://your-url" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#70B7FD; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/codeblog/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">若水的前端博客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">最优秀的模式往往是最简单的东西</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/codeblog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/codeblog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/codeblog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/codeblog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://skycoin.gitee.io/codeblog/2021/02/27/%E5%9F%BA%E6%9C%AC%E5%BC%95%E7%94%A8%E7%B1%BB%E5%9E%8B/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=4083356047,3551369852&fm=26&gp=0.jpg">
      <meta itemprop="name" content="若说">
      <meta itemprop="description" content="立志完成前端知识体系的架构">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="若水的前端博客">
    </span>
      <header class="post-header">
      
        <i class="fa fa-thumb-tack"></i>
        <font color=7D26CD>置顶</font>
      
        <h1 class="post-title" itemprop="name headline">
          Javascript基础（四）：javascript基本引用类型
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-02-27 10:14:52" itemprop="dateCreated datePublished" datetime="2021-02-27T10:14:52+08:00">2021-02-27</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-04-15 09:25:56" itemprop="dateModified" datetime="2021-04-15T09:25:56+08:00">2021-04-15</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/codeblog/categories/%E5%89%8D%E7%AB%AF%E6%A1%86%E6%9E%B6/" itemprop="url" rel="index"><span itemprop="name">前端框架</span></a>
                </span>
            </span>

          
            <span id="/codeblog/2021/02/27/%E5%9F%BA%E6%9C%AC%E5%BC%95%E7%94%A8%E7%B1%BB%E5%9E%8B/" class="post-meta-item leancloud_visitors" data-flag-title="Javascript基础（四）：javascript基本引用类型" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/codeblog/2021/02/27/%E5%9F%BA%E6%9C%AC%E5%BC%95%E7%94%A8%E7%B1%BB%E5%9E%8B/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/codeblog/2021/02/27/%E5%9F%BA%E6%9C%AC%E5%BC%95%E7%94%A8%E7%B1%BB%E5%9E%8B/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>[toc]</p>
<h2 id="1-理解对象"><a href="#1-理解对象" class="headerlink" title="1.理解对象"></a>1.理解对象</h2><p>引用值（或者对象）是某个特定引用类型的实例。在 es 中，引用类型是把数据和功能组织到一起的结构，经常被人错误的称为类。虽然从技术上来讲 js 是一门面向对象语言，但 es 却少传统的面向对象编程所具备的某些基本结构，包括类和接口。引用类型有时候也被称为对象定义，因为它们描述了自己的对象应有的属性和方法</p>
<p>对象被认为是某个特定引用类型的实例。新对象通过使用 new 操作符后跟一个构造函数来创建，构造函数就是用来创建新对象的函数</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> now = <span class="keyword">new</span> <span class="built_in">Date</span>();</span><br></pre></td></tr></table></figure>

<p>这行代码创建了引用类型 Date 的一个新实例，并将它保存在变量 now 中，Date()在这里是构造函数，它负责创建一个只有默认属性和方法的简单对象，es 提供了很多像 Date 这样的原生引用类。</p>
<p>1.1.1 Date</p>
<p>Date 类型将日期保存为自协调世界时(UTC,Universal Time coordinated)时间 1970 年 1 月 1 日零点。</p>
<p>要创建日期对象，就要使用 new 操作符来调用 Date 构造函数</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> now = <span class="keyword">new</span> <span class="built_in">Date</span>()</span><br><span class="line"><span class="comment">// 必须要传毫秒数</span></span><br><span class="line"><span class="built_in">Date</span>.parse()方法接收一个表示日期字符串参数</span><br><span class="line"><span class="built_in">Date</span>.parse()</span><br><span class="line"><span class="built_in">Date</span>.parse(<span class="string">&quot;3-7-2013&quot;</span>)</span><br><span class="line"><span class="built_in">Date</span>.parse(<span class="string">&quot;03-07-2013&quot;</span>) <span class="comment">//返回时间戳</span></span><br></pre></td></tr></table></figure>

<p>要创建一个“2019 年 5 月 23 日”日期对象</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> someDate = <span class="keyword">new</span> <span class="built_in">Date</span>(<span class="built_in">Date</span>.parse(<span class="string">&quot;May 23,2019&quot;</span>));</span><br><span class="line">===</span><br><span class="line"><span class="keyword">let</span> someDate = <span class="keyword">new</span> <span class="built_in">Date</span>(<span class="string">&quot;May 23,2019&quot;</span>)</span><br></pre></td></tr></table></figure>

<h3 id="1-1-1-继承的方法"><a href="#1-1-1-继承的方法" class="headerlink" title="1.1.1 继承的方法"></a>1.1.1 继承的方法</h3><p>与其他类型一样，Date 类型重写了 toLocaleString()、toString（）和 valueOf()方法，但与其他类型不同，重写后这样方法的返回值不一样。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">getTime(); <span class="comment">// 返回日期的毫秒表示；与valueof相同</span></span><br></pre></td></tr></table></figure>

<h3 id="1-1-2-RegExp"><a href="#1-1-2-RegExp" class="headerlink" title="1.1.2 RegExp"></a>1.1.2 RegExp</h3><p>es 通过 RegExp 类型支持正则表达式。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> expression = <span class="regexp">/pattern/</span>afgls;</span><br></pre></td></tr></table></figure>

<p>每个正则表达式可以带零个或多个 flags（标记），用于控制正则表达式的行为。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">g:全局模式，表示查找字符串的全部内容，而不是找到第一个匹配的内容</span><br><span class="line">i:不区分大小写</span><br><span class="line">m：多行模式，表示查找到一行文本末尾会继续查找</span><br><span class="line">y：粘附模式</span><br><span class="line">u:unicode模式</span><br><span class="line">s:匹配任何字符</span><br><span class="line"></span><br><span class="line"><span class="comment">//匹配字符串中的所有 at</span></span><br><span class="line"><span class="keyword">let</span> pattern1 = <span class="regexp">/at/g</span>;</span><br></pre></td></tr></table></figure>

<h3 id="1-2-原始值包装类型"><a href="#1-2-原始值包装类型" class="headerlink" title="1.2 原始值包装类型"></a>1.2 原始值包装类型</h3><p>为了方便操作原始值，es 提供了 3 中特殊的引用类型:Boolean、Number 和 string.每当使用到某个原始值的方法或属性时，后台都会创建一个相应原始包装类型的对象，从而暴露出操作原始值的各种方法</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> s1 = <span class="string">&quot;some&quot;</span>;</span><br><span class="line"><span class="keyword">let</span> s2 = s1.substring(<span class="number">2</span>);</span><br><span class="line"><span class="comment">// s1是一个包含字符串的变量，它是一个原始值。第二行紧接着在s1上调用了substring方法，并把结果</span></span><br><span class="line"><span class="comment">// 保存在s2中，原始值本身不是对象，因此逻辑上不应该有方法。而实际上后台进行了很多处理，从而实现上述操作。具体来说当第二行访问s1时，是以已读模式访问的，也就是要从内存中读取变量保存的值</span></span><br></pre></td></tr></table></figure>

<p>在以读模式访问访问字符串的任何时候，后台都会执行这三步</p>
<ul>
<li>创建一个 String 类型的实例</li>
<li>调用实例上的特定方法</li>
<li>销毁实例</li>
</ul>
<p>可以把这 3 步想象成执行了下面 3 行 es 代码</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> s1 = <span class="keyword">new</span> <span class="built_in">String</span>(<span class="string">&quot;some text&quot;</span>);</span><br><span class="line"><span class="keyword">let</span> s2 = s1.substring(<span class="number">2</span>);</span><br><span class="line">s1 = <span class="literal">null</span>;</span><br></pre></td></tr></table></figure>

<p>这种行为可以让原始值拥有对象的行为。</p>
<p>引用类型与原始值包装类型的主要区别在于对象的生命周期。在通过 new 实例化引用类型后，得到的实例会在离开作用域时被销毁，而在自动创建的原始值包装对象则只存在于访问它的那行代码的执行期间。这意味着不能在运行时给原始值添加属性</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> s1 = <span class="string">&quot;some&quot;</span>;</span><br><span class="line">s1.color = <span class="string">&quot;red&quot;</span>;</span><br><span class="line"><span class="built_in">console</span>.log(s1.color); <span class="comment">// undefined</span></span><br></pre></td></tr></table></figure>

<p>解读：在非严格模式下，当我们尝试给原始值赋值时，其实是生成了一个原始值包装类型实例，也是给这个新生成的实例进行赋值，但在赋值结束之后，这个实例就会被销毁了。实际上并没有把值赋值给我们想要的目标变量上，当我们下次再读取这个属性的时候，其实是又创建了一个原始值包装类型实例，显然这个实例不是和之前的实例一致，所以自然读取不到属性值</p>
<p>可以显式的使用 Boolean、Number 和 String 构造函数创建原始值包装对象。不过应该在确实必要在这样做，否则会让开发产生疑惑，分不清是原始值还是引用值。在原始值包装类型的实例上调用 typeOf 会返回 object，所有原始值包装对象都会转换为布尔值 true</p>
<p>另外，Object 构造函数作为一个工厂方法，能够根据传入值的类型返回相应的原始包装类型实例</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> obj = <span class="keyword">new</span> <span class="built_in">Object</span>(<span class="string">&quot;some&quot;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(obj <span class="keyword">instanceof</span> string); <span class="comment">// true</span></span><br><span class="line"><span class="comment">// 如果传递给object是字符串，则会创建一个String实例</span></span><br><span class="line"><span class="comment">// 传递数值，则会创建Number实例</span></span><br><span class="line"><span class="comment">// 传递布尔值，则会得到Boolean的实例</span></span><br></pre></td></tr></table></figure>

<h4 id="1-2-1-Boolean"><a href="#1-2-1-Boolean" class="headerlink" title="1.2.1 Boolean"></a>1.2.1 Boolean</h4><p>Boolean 对应布尔值的引用类型。要创建一个布尔对象，就是用 Boolean 构造函数传入 true 或 false</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> booleanObject = <span class="keyword">new</span> <span class="built_in">Boolean</span>(<span class="literal">true</span>);</span><br></pre></td></tr></table></figure>

<p>Boolean 的实例会重写 valueOf 方法，返回一个 true 或 false。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> falseObject = <span class="keyword">new</span> <span class="built_in">Boolean</span>(<span class="literal">false</span>);</span><br><span class="line"><span class="keyword">let</span> res = falseObject &amp;&amp; <span class="literal">true</span>;</span><br><span class="line"><span class="built_in">console</span>.log(res); <span class="comment">// true</span></span><br><span class="line"><span class="keyword">let</span> falseValue = <span class="literal">false</span>;</span><br><span class="line">res = falseValue &amp;&amp; <span class="literal">true</span>;</span><br><span class="line"><span class="built_in">console</span>.log(res); <span class="comment">// false</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typeof</span> falseObject; <span class="comment">//object</span></span><br><span class="line"><span class="keyword">typeof</span> falseValue; <span class="comment">// boolean</span></span><br><span class="line">falseObject <span class="keyword">instanceof</span> <span class="built_in">Boolean</span>; <span class="comment">// true</span></span><br><span class="line">falseValue <span class="keyword">instanceof</span> <span class="built_in">Boolean</span>; <span class="comment">// false</span></span><br></pre></td></tr></table></figure>

<p>注意：所有对象在布尔表达式中都会自动转换为 true,因此 falseObject 在这个表达式里是加上表示一个 true 值。</p>
<p>除此之外，原始值和引用值（Boolean 对象）还有几个区别</p>
<p>typeof 操作符对原始值返回 boolean，但对引用值返回 object</p>
<h4 id="1-2-2-Number"><a href="#1-2-2-Number" class="headerlink" title="1.2.2 Number"></a>1.2.2 Number</h4><p>Number 是对应数值的引用类型。要创建一个 Number 对象，就要使用 Number 构造函数并传入一个数值</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> numberObject = <span class="keyword">new</span> <span class="built_in">Number</span>(<span class="number">10</span>);</span><br></pre></td></tr></table></figure>

<p>与 Boolean 一样，Number 类型重写了 valueOf()、toLocalString()和 toString()方法。valueOf()方法返回 Number 对象表示的原始数值。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> num = <span class="number">10</span>;</span><br><span class="line">num.toString(); <span class="comment">// &quot;10&quot;</span></span><br><span class="line">num.toString(<span class="number">2</span>); <span class="comment">// &quot;1010&quot;</span></span><br><span class="line">num.toString(<span class="number">8</span>); <span class="comment">// &quot;12&quot;</span></span><br><span class="line">num.toString(<span class="number">10</span>); <span class="comment">// 10</span></span><br><span class="line">num.toString(<span class="number">16</span>); <span class="comment">// a</span></span><br></pre></td></tr></table></figure>

<p>toFixed()方法返回包含指定小数点的数值字符串.如果数值本身的小数位超过参数指定的位数，则四舍五入到最接近的小数位</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> num = <span class="number">10.005</span>;</span><br><span class="line">num.toFixed(<span class="number">2</span>); <span class="comment">// &quot;10.01&quot;</span></span><br><span class="line"><span class="comment">// 可以用来处理货币</span></span><br></pre></td></tr></table></figure>

<p>toExponential()返回科学计数法表示数值字符串</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> num = <span class="number">10</span>;</span><br><span class="line">num.toExponential(<span class="number">1</span>); <span class="comment">//&quot;1.0e+1&quot;  科学计数法</span></span><br></pre></td></tr></table></figure>

<p>toPrecision 方法会根据情况返回最合理的输出结果，可能是固定长度，也可能是科学计数法形式。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> num = <span class="number">99</span>;</span><br><span class="line">num.toPrecision(<span class="number">1</span>); <span class="comment">// &quot;1e+2&quot;</span></span><br><span class="line">num.toPrecision(<span class="number">2</span>); <span class="comment">// &quot;99&quot;</span></span><br><span class="line">num.toPrecision(<span class="number">3</span>); <span class="comment">// &quot;99.0&quot;</span></span><br></pre></td></tr></table></figure>

<h4 id="1-2-3-String"><a href="#1-2-3-String" class="headerlink" title="1.2.3 String"></a>1.2.3 String</h4><p>对应字符串的引用类型，String 对象的方法可以在所有字符串原始值上调用。3 个继承的方法 valueOf()、toLocaleString()和 toString()都返回对象的原始字符串</p>
<p>每个 string 对象都有一个 length 属性，表示字符串中字符串字符的数量</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> stringValue = <span class="string">&quot;hello world&quot;</span>;</span><br><span class="line">stringValue.length; <span class="comment">// 11</span></span><br></pre></td></tr></table></figure>

<p>string 类型提供了很多方法解析和操作字符串</p>
<ul>
<li>js 字符</li>
</ul>
<p>js 字符串由 16 位码元组成。对于数字符来说，每 16 位码元对应一个字符，换句话来说字符串的 length 属性表示字符串包含多少个码元</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> message = <span class="string">&quot;abcd&quot;</span>;</span><br><span class="line">message.length; <span class="comment">// 5</span></span><br></pre></td></tr></table></figure>

<p>另外 charAt()方法返回给定索引位置的字符，由传给方法的整数参数指定。</p>
<p>字符串的操作方法</p>
<ul>
<li>Concat()</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> res = <span class="string">&quot;aaa&quot;</span>.concat(<span class="string">&quot;bbb&quot;</span>);</span><br></pre></td></tr></table></figure>

<ul>
<li>es 提供了 3 个从字符串中提取字符串的方法: slice()、substr()、substring()</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">&#x27;hello world&#x27;</span></span><br><span class="line">str.slice(<span class="number">3</span>) <span class="comment">// loworld</span></span><br><span class="line">str.substring(<span class="number">3</span>) <span class="comment">//</span></span><br><span class="line">str.substr(<span class="number">3</span>) <span class="comment">//</span></span><br><span class="line"></span><br><span class="line">str.slice(<span class="number">3</span>,<span class="number">7</span>) <span class="comment">// lo w</span></span><br><span class="line">str.substring(<span class="number">3</span>,<span class="number">7</span>) <span class="comment">// lo w</span></span><br><span class="line">str.substr(<span class="number">3</span>,<span class="number">7</span>) <span class="comment">// lo worl</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 当某个值是负值的时,slice()方法将所有负值参数都当成字符串长度加上负参数值</span></span><br><span class="line"><span class="comment">// substr()方法将第一个负参数值当成字符串长度加上该值，将第二个负参数值转换为0</span></span><br><span class="line"><span class="comment">//substring()将所有负参数值都转换为0</span></span><br><span class="line"><span class="keyword">let</span> str = <span class="string">&#x27;hello world&#x27;</span></span><br><span class="line"></span><br><span class="line">str.slice(-<span class="number">3</span>) <span class="string">&#x27;rld&#x27;</span></span><br><span class="line">str.substring(-<span class="number">3</span>) <span class="string">&#x27;hello world&#x27;</span></span><br><span class="line">str.substr(-<span class="number">3</span>) <span class="string">&#x27;rld&#x27;</span></span><br><span class="line">str.slice(<span class="number">3</span>,-<span class="number">4</span>) <span class="string">&#x27;low&#x27;</span></span><br><span class="line">str.substring(<span class="number">3</span>,-<span class="number">4</span>) <span class="string">&#x27;hel&#x27;</span></span><br><span class="line">str.substr(<span class="number">3</span>,-<span class="number">4</span>)  <span class="string">&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>

<p>Slice 和 subStr 和 subString 区别</p>
<p>相同点</p>
<ul>
<li>都不会改变原始字符串</li>
<li>第一个参数都是指定字符串开始的位置</li>
<li>第二个参数不传则将字符串的尾部作为结束位置</li>
</ul>
<p>不同点</p>
<ul>
<li><p>Slice():第一个参数指定字符串的开始位置，第二个参数指定字符串的结束位置</p>
</li>
<li><p>Substring()会将小的参数作为开始位置，大的位置作为结束位置</p>
</li>
<li><p>Substr（）：第一个参数指定字符串开始的位置，第二个参数要返回的字符个数</p>
</li>
<li><p>Substring（）：参数为负值时，会将所有的负值参数转换成 0</p>
</li>
<li><p>substr 方法将负的第一个参数加上字符传的长度得到开始位置，将负的第二个参数转换 0</p>
</li>
<li><p>字符串位置方法</p>
</li>
</ul>
<p>有两个方法用于在字符串中定位子字符串:indexOf()和 lastIndexOf()。这两个方法从字符串搜索传入的字符串，并返回位置。</p>
<p>区别在于：indexOf()的方法从字符串开头开始查找字符串，而 lastIndexOf（）方法从字符串末尾开始查找字符串</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">&quot;hello world&quot;</span>;</span><br><span class="line">str.indexOf(<span class="string">&quot;o&quot;</span>); <span class="comment">// 4</span></span><br><span class="line">str.lastIndexOf(<span class="string">&quot;o&quot;</span>); <span class="comment">// 7</span></span><br></pre></td></tr></table></figure>

<p>这两个方法都可以接受可选的第二个参数，表示开始搜索的位置。这意味着，indexOf（）会从这个参数指定的位置开始像字符串末尾搜索，忽略该位置之前的字符，lastIndexOf()则会从这个参数指定的位置开始向字符串开头搜索。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">&quot;hello world&quot;</span>;</span><br><span class="line">str.indexOf(<span class="string">&quot;o&quot;</span>, <span class="number">6</span>); <span class="comment">// 7</span></span><br><span class="line">str.lastIndexOf(<span class="string">&quot;o&quot;</span>, <span class="number">6</span>); <span class="comment">//4</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 进行搜索的时候可以使用</span></span><br><span class="line"><span class="keyword">let</span> str = <span class="string">&quot;xxxxxx...&quot;</span>;</span><br><span class="line"><span class="keyword">let</span> pos = str.indexOf(<span class="string">&quot;x&quot;</span>);</span><br><span class="line"><span class="keyword">let</span> a = [];</span><br><span class="line"><span class="keyword">while</span> (pos &gt; -<span class="number">1</span>) &#123;</span><br><span class="line">  a.push(pos);</span><br><span class="line">  pos = str.indexOf(<span class="string">&quot;x&quot;</span>, pos + <span class="number">1</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>字符串包含方法</li>
</ul>
<p>es 增加 3 个用于判断字符串中包含另一个字符串的方法：startWith()、endsWith()、includes()</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> msg = <span class="string">&quot;foobarbaz&quot;</span>;</span><br><span class="line">msg.startWith(<span class="string">&quot;foo&quot;</span>); <span class="comment">// true</span></span><br><span class="line">msg.startWith(<span class="string">&quot;bar&quot;</span>); <span class="comment">// false</span></span><br><span class="line">msg.endsWith(<span class="string">&quot;baz&quot;</span>); <span class="comment">// true</span></span><br><span class="line">msg.includes(<span class="string">&quot;bar&quot;</span>); <span class="comment">// true</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 第二个参数表示开始搜索的位置</span></span><br><span class="line">msg.startsWith(<span class="string">&quot;foo&quot;</span>, <span class="number">1</span>); <span class="comment">//false</span></span><br><span class="line">msg.endsWith(<span class="string">&quot;bar&quot;</span>, <span class="number">6</span>); <span class="comment">//从后往前进行数</span></span><br></pre></td></tr></table></figure>

<ul>
<li>Trim()方法</li>
</ul>
<p>这个方法会创建一个副本，删除前后所有的空格符，在返回结果</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">&quot;    heloo woeld&quot;</span>;</span><br><span class="line"><span class="keyword">let</span> str1 = str.trim();</span><br><span class="line"><span class="comment">// 	由于trim()返回的是字符串的副本，因此原始字符串不受影响，因此原始字符串不受影响</span></span><br></pre></td></tr></table></figure>

<ul>
<li>Repeat()方法</li>
</ul>
<p>es 在所有字符串上都提供了 repeat()方法。这个方法接受一个整数参数，表示要将字符串赋值多少次，然后拼接所有副本的结果</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">&quot;na. &quot;</span>;</span><br><span class="line">str.repeat(<span class="number">3</span>);</span><br></pre></td></tr></table></figure>

<ul>
<li>padStart 和 padEnd()方法</li>
</ul>
<p>padStart()和 padEnd()方法会复制字符串，，如果小于指定长度，则在一边填充字符</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">&quot;foo&quot;</span>;</span><br><span class="line"><span class="keyword">let</span> b = str.padStart(<span class="number">6</span>);</span><br><span class="line">b.length; <span class="comment">// 6</span></span><br><span class="line"></span><br><span class="line">b.padEnd(<span class="number">9</span>, <span class="string">&quot;.&quot;</span>);</span><br><span class="line">(<span class="string">&quot;   aaa...&quot;</span>);</span><br></pre></td></tr></table></figure>

<ul>
<li>字符串的迭代和解构</li>
</ul>
<p>字符串的原型上暴露了一个@@iterator 方法，表示可以迭代字符串的每个字符。可以像下面这样手动使用迭代器</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> msg = <span class="string">&quot;abc&quot;</span>;</span><br><span class="line"><span class="keyword">let</span> strIterator = msg[<span class="built_in">Symbol</span>.iterator]();</span><br><span class="line">strIterator.next(); <span class="comment">// &#123;value:&#x27;a&#x27;,done:false&#125;</span></span><br><span class="line">strIterator.next(); <span class="comment">// &#123;value:&#x27;b&#x27;,done:false&#125;</span></span><br><span class="line">strIterator.next(); <span class="comment">//&#123;value:&#x27;c&#x27;,done:false&#125;</span></span><br><span class="line">strIterator.next(); <span class="comment">//&#123;value:undefined,done:false&#125;</span></span><br></pre></td></tr></table></figure>

<p>在 for..of..循环中可以通过这个迭代器按序访问每个字符</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> (<span class="keyword">const</span> c <span class="keyword">of</span> <span class="string">&quot;abcde&quot;</span>) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(c);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>有了这个迭代器之后，字符串就可以通过解构操作符来解构了，比如，可以方便的把字符串分割为字符数组</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> msg = <span class="string">&#x27;abcd&#x27;</span></span><br><span class="line">[...msg] <span class="comment">// [&#x27;a&#x27;,&#x27;b&#x27;,&#x27;c&#x27;,&#x27;d&#x27;]</span></span><br></pre></td></tr></table></figure>

<ul>
<li>字符串大小写转换</li>
</ul>
<p>下一组方法涉及大小写转换，包括四个方法</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">toLowerCase();</span><br><span class="line">toLocaleLowerCase();</span><br><span class="line">toUpperCase();</span><br><span class="line">toLocaleUpperCase();</span><br><span class="line">toLowerCase();</span><br><span class="line">toUpperCase();</span><br></pre></td></tr></table></figure>

<ul>
<li>split</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> colorText = <span class="string">&quot;red,green,blue,yellow&quot;</span>;</span><br><span class="line"><span class="keyword">let</span> color1 = colorText.split(<span class="string">&quot;,&quot;</span>);</span><br><span class="line"><span class="keyword">let</span> color2 = colorText.split(<span class="string">&quot;,&quot;</span>, <span class="number">2</span>);</span><br></pre></td></tr></table></figure>

<ul>
<li>localCompare（）方法</li>
</ul>
<p>如果按照字母表顺序，字符串应该排在字符串参数前头，则返回负值</p>
<p>如果字符串与字符串参数相等，则返回 0</p>
<p>如果按照字母表顺序，字符串应该排在字符串参数的后头，则返回正值</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">&quot;yellow&quot;</span>;</span><br><span class="line">str.localeCompare(<span class="string">&quot;brick&quot;</span>); <span class="comment">// 1</span></span><br></pre></td></tr></table></figure>

<h3 id="1-3-单例内置对象"><a href="#1-3-单例内置对象" class="headerlink" title="1.3 单例内置对象"></a>1.3 单例内置对象</h3><p>对内置对象的定义是：任何由 es 实现提供、与宿主环境无关，并在 es 程序开始执行时就存在的对象</p>
<p>包括 Object、Array 和 String，本节介绍另外两个单列内置对象：Global 和 Math</p>
<p>Global</p>
<p>Global 对象时 es 中最特别的对象，因为代码不会显式的访问它。规定的 Global 对象为一种兜底对象，它所针对的是不属于任何对象的属性和方法。事实上，不存在全局变量或全局函数这种东西。在全局作用域中定义的变量和函数都会变成 Global 对象的属性。前面的函数 isNaN()、isFinite()、parseInt()和 parseFloat()，实际上都是 Global 对象的方法。除了这些还有另外的一些方法</p>
<ul>
<li>URL 编码方法</li>
</ul>
<p>encodeURI()和 encodeURIComponent()方法用于编码统一资源标识符（URI)，以便传给浏览器。有效的 URI 不能包含某些字符，比如空格。</p>
<p>EncodeURI()方法用于对整个 URI 进行编码。这两个方法的主要区别是,encodeURI()不会编码 URL 组件特殊的字符</p>
<p>相对的是 deocdeURI. 和 decodeURIComponent</p>
<ul>
<li>eval 方法</li>
</ul>
<p>最后一个方法可能是整个 es 语言中最强大的，它就是 eval（），这个方法是一个完整的 es 解释器，它接收一个参数，即一个要执行的 es</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">eval</span>(<span class="string">&#x27;console.log(&#x27;</span>hi<span class="string">&#x27;)&#x27;</span>)</span><br></pre></td></tr></table></figure>

<p>通过 eval()执行的代码属于该调用所在上下文，被执行的代码与该上下文拥有相同的作用域链，这意味着定义在包含上下文变量可以在 eval()调用内部被引用</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> msg = <span class="string">&quot;hello world&quot;</span>;</span><br><span class="line"><span class="built_in">eval</span>(<span class="string">&quot;console.log(msg)&quot;</span>);</span><br></pre></td></tr></table></figure>

<ul>
<li>Global 对象属性</li>
</ul>
<p>Global 对象有很多属性，其中一些前面已经提过了，像 undefined、NaN 和 Infinity 等特殊值都是 Global 对象的属性。此外，所有原生引用类型构造函数，比图 Object 和 Function 也都是 Global 对象的属性。</p>
<ul>
<li>window 对象</li>
</ul>
<p>虽然没有规定直接访问 Global 对象的方式，但浏览器将 window 对象实现为 Global 对象的代理。因此，所有全局作用域中声明的变量和函数都变成了 window 属性</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> color = <span class="string">&quot;red&quot;</span>;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">sayColor</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="built_in">window</span>.color);</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">window</span>.sayColor();</span><br><span class="line"><span class="comment">// 这里定义了一个名为color的全局变量和一个名为sayColor的全局函数</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 另一种获取Global对象的方式使用如下</span></span><br><span class="line"><span class="keyword">let</span> <span class="built_in">global</span> = (<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="built_in">this</span>;</span><br><span class="line">&#125;)();</span><br></pre></td></tr></table></figure>

<ul>
<li>Math</li>
</ul>
<p>es 提供了 Math 对象作为保存数学公式、信息和计算的地方。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Math</span>.min();</span><br><span class="line"><span class="built_in">Math</span>.max();</span><br><span class="line"><span class="built_in">Math</span>.ceil(); <span class="comment">// 始终向上舍入为最接近的整数</span></span><br><span class="line"><span class="built_in">Math</span>.floor(); <span class="comment">// 方法始终向下舍入为最近的整数</span></span><br><span class="line"><span class="built_in">Math</span>.round(); <span class="comment">// 方法执行四舍五入</span></span><br><span class="line"><span class="built_in">Math</span>.fround(); <span class="comment">// 返回数值最接近的单精度</span></span><br></pre></td></tr></table></figure>

<ul>
<li>random 方法</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Math</span>.random()方法返回一个<span class="number">0</span>-<span class="number">1</span>范围内的随机数，其中包<span class="number">0</span>不包<span class="number">1</span></span><br></pre></td></tr></table></figure>

<h2 id="小结"><a href="#小结" class="headerlink" title="小结"></a>小结</h2><p>js 中的对象称为引用值，几种内置的引用类型可用于创建特定类型的对象</p>
<ul>
<li>引用值与传统面向对象编程语言的类相似，但实现不同</li>
<li>Date 类型提供关于日期和时间的计算</li>
<li>RegExp 类型是 es 支持正则表达式的接口</li>
</ul>
<p>js 比较独特的一点就是，函数实际上是 Function 类型的实例，，也就是说函数是对象，因为是对象，所以函数有方法，可以增强能力</p>
<p>由于原始值包装类型存在，js 中的原始值可以被当成对象来使用。有 3 种原始包装类型：Boolean、Number 和 String</p>
<ul>
<li>每种包装类型都映射到同名的原始类型</li>
<li>以读模式访问原始值时，后台会实例化一个原始值包装类型的对象，借助这个可以对象可以操作相应的数据</li>
<li>涉及原始值的语句执行完毕后，包装对象就会被销毁</li>
</ul>

    </div>

    
    
    
      <div>
          
            
<div class="my_post_copyright">
  <script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>
  
  <!-- JS库 sweetalert 可修改路径 -->
  <script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
  <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
  <p><span>本文标题:</span><a href="/codeblog/2021/02/27/%E5%9F%BA%E6%9C%AC%E5%BC%95%E7%94%A8%E7%B1%BB%E5%9E%8B/">Javascript基础（四）：javascript基本引用类型</a></p>
  <p><span>文章作者:</span><a href="/" title="访问  的个人博客"></a></p>
  <p><span>发布时间:</span>2021年02月27日 - 10:02</p>
  <p><span>最后更新:</span>2021年04月15日 - 09:04</p>
  <p><span>原始链接:</span><a href="/codeblog/2021/02/27/%E5%9F%BA%E6%9C%AC%E5%BC%95%E7%94%A8%E7%B1%BB%E5%9E%8B/" title="Javascript基础（四）：javascript基本引用类型">http://skycoin.gitee.io/codeblog/2021/02/27/%E5%9F%BA%E6%9C%AC%E5%BC%95%E7%94%A8%E7%B1%BB%E5%9E%8B/</a>
    <span class="copy-path"  title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="http://skycoin.gitee.io/codeblog/2021/02/27/%E5%9F%BA%E6%9C%AC%E5%BC%95%E7%94%A8%E7%B1%BB%E5%9E%8B/"  aria-label="复制成功！"></i></span>
  </p>
  <p><span>许可协议:</span><i class="fa fa-creative-commons"></i> 转载请保留原文链接及作者。</p>  
</div>
<script> 
    var clipboard = new Clipboard('.fa-clipboard');
    $(".fa-clipboard").click(function(){
      clipboard.on('success', function(){
        swal({   
          title: "",   
          text: '复制成功',
          icon: "success", 
          showConfirmButton: true
          });
    });
    });  
</script>



          
    </div>
        <div class="reward-container">
  <div>Donate comment here.</div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/codeblog/images/wechatpay.png" alt="若说 微信支付">
        <p>微信支付</p>
      </div>
      
      <div style="display: inline-block;">
        <img src="/codeblog/images/alipay.png" alt="若说 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

        

  <div class="followme">
    <p>欢迎关注我的其它发布渠道</p>

    <div class="social-list">

        <div class="social-item">
          <a target="_blank" class="social-link" href="/images/wechat_channel.jpg">
            <span class="icon">
              <i class="fab fa-weixin"></i>
            </span>

            <span class="label">WeChat</span>
          </a>
        </div>

        <div class="social-item">
          <a target="_blank" class="social-link" href="/atom.xml">
            <span class="icon">
              <i class="fa fa-rss"></i>
            </span>

            <span class="label">RSS</span>
          </a>
        </div>
    </div>
  </div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/codeblog/tags/%E5%89%8D%E7%AB%AF/" rel="tag"># 前端</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/codeblog/2021/02/27/%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5/" rel="prev" title="Javascript基础（二）：javascript基本概念">
      <i class="fa fa-chevron-left"></i> Javascript基础（二）：javascript基本概念
    </a></div>
      <div class="post-nav-item">
    <a href="/codeblog/2021/02/27/%E5%8F%98%E9%87%8F%E4%BD%9C%E7%94%A8%E5%9F%9F%E5%92%8C%E5%86%85%E5%AD%98/" rel="next" title="Javascript基础（三）：javascript的变量、作用域与内存">
      Javascript基础（三）：javascript的变量、作用域与内存 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E7%90%86%E8%A7%A3%E5%AF%B9%E8%B1%A1"><span class="nav-number">1.</span> <span class="nav-text">1.理解对象</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-1-%E7%BB%A7%E6%89%BF%E7%9A%84%E6%96%B9%E6%B3%95"><span class="nav-number">1.1.</span> <span class="nav-text">1.1.1 继承的方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-2-RegExp"><span class="nav-number">1.2.</span> <span class="nav-text">1.1.2 RegExp</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-%E5%8E%9F%E5%A7%8B%E5%80%BC%E5%8C%85%E8%A3%85%E7%B1%BB%E5%9E%8B"><span class="nav-number">1.3.</span> <span class="nav-text">1.2 原始值包装类型</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-1-Boolean"><span class="nav-number">1.3.1.</span> <span class="nav-text">1.2.1 Boolean</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-2-Number"><span class="nav-number">1.3.2.</span> <span class="nav-text">1.2.2 Number</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-3-String"><span class="nav-number">1.3.3.</span> <span class="nav-text">1.2.3 String</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-%E5%8D%95%E4%BE%8B%E5%86%85%E7%BD%AE%E5%AF%B9%E8%B1%A1"><span class="nav-number">1.4.</span> <span class="nav-text">1.3 单例内置对象</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%B0%8F%E7%BB%93"><span class="nav-number">2.</span> <span class="nav-text">小结</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="若说"
      src="https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=4083356047,3551369852&fm=26&gp=0.jpg">
  <p class="site-author-name" itemprop="name">若说</p>
  <div class="site-description" itemprop="description">立志完成前端知识体系的架构</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/codeblog/archives/">
        
          <span class="site-state-item-count">5</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/codeblog/categories/">
          
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/lovebxy" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;lovebxy" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="/codeblog/1490365616@qq.com" title="E-Mail → 1490365616@qq.com"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://weibo.com/yourname" title="Weibo → https:&#x2F;&#x2F;weibo.com&#x2F;yourname" rel="noopener" target="_blank"><i class="fab fa-weibo fa-fw"></i>Weibo</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://plus.google.com/yourname" title="Google → https:&#x2F;&#x2F;plus.google.com&#x2F;yourname" rel="noopener" target="_blank"><i class="fab fa-google fa-fw"></i>Google</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      Links
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="http://yoursite.com/" title="http:&#x2F;&#x2F;yoursite.com" rel="noopener" target="_blank">test</a>
        </li>
    </ul>
  </div>

      </div>
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">若说</span>
</div>

<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共25.5k字</span>
</div>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<div class="powered-by">
  <i class="fa fa-user-md"></i>
  <span id="busuanzi_container_site_pv">
    本站访问量:<span id="busuanzi_value_site_pv"></span>次
  </span>
  <span class="post-meta-divider">|</span>
  <span id="busuanzi_container_site_uv">
    本站总访客量：<span id="busuanzi_value_site_uv"></span>人
  </span>
</div>

        








      </div>
    </footer>
  </div>

  
  <script src="/codeblog/lib/anime.min.js"></script>
  <script src="/codeblog/lib/velocity/velocity.min.js"></script>
  <script src="/codeblog/lib/velocity/velocity.ui.min.js"></script>

<script src="/codeblog/js/utils.js"></script>

<script src="/codeblog/js/motion.js"></script>


<script src="/codeblog/js/schemes/pisces.js"></script>


<script src="/codeblog/js/next-boot.js"></script>




  




  
<script src="/codeblog/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : true,
      notify     : false,
      appId      : 'DbTfuJfplEWGguVvLyabTu99-gzGzoHsz',
      appKey     : 'hkLIIwmXwOd95taiLRxnUrjO',
      placeholder: "Just go go",
      avatar     : 'monsterid',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : true,
      lang       : '' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

  <script type="text/javascript" src="/js/src/clicklove.js"></script>
  <script type="text/javascript" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>

</body>
</html>
